//
//  Team9ExpertHeaderView.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/15.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertHeaderView.h"
#import "Team9Header.h"
#import "Team9BaseLabel.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "Team9ExpertPro_serviceModel.h"
#import "Team9ExpertUser_home_infoModel.h"
#import "Team9SaveModel.h"
#import "DataBaseTool.h"

@interface Team9ExpertHeaderView ()
@property(nonatomic, strong)Team9BaseView *headView;
@property(nonatomic, strong)UIImageView *faceImageView;
@property(nonatomic, strong)Team9BaseLabel *workCountLabel;
@property(nonatomic, strong)Team9BaseLabel *reViewLabel;
@property(nonatomic, strong)UILabel *typeLabel;
@property(nonatomic, strong)UIView *lineView;
@property(nonatomic, strong)Team9BaseLabel *nameLabel;
@property(nonatomic, strong)UIButton *collectionButton;

@property(nonatomic, strong)Team9BaseView *footView;
@property(nonatomic, strong)Team9BaseLabel *loctionLabel;
@property(nonatomic, strong)Team9BaseLabel *costLabel;
@property(nonatomic, strong)Team9BaseLabel *rangelabel;
@property(nonatomic, strong)UIButton *detialButton;
@property(nonatomic, strong)Team9SaveModel *saver;

@end
// 70 + 50
@implementation Team9ExpertHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        self.serviceModel = [Team9ExpertPro_serviceModel new];
        self.userModel = [Team9ExpertUser_home_infoModel new];
//        [self test];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changecolor) name:@"changcolor" object:nil];
    }
    return self;
}
- (void)changecolor
{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.backgroundColor = [UIColor blackColor];
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
}
- (void)createView{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.backgroundColor = [UIColor colorWithWhite:0.15 alpha:1];
    } else self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    self.headView = [[Team9BaseView alloc] init];
    [self addSubview:_headView];
//    self.headView.backgroundColor = [UIColor whiteColor];
    
    self.faceImageView = [UIImageView new];
    [self addSubview:_faceImageView];
    
    self.nameLabel = [[Team9BaseLabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    [self addSubview:_nameLabel];
    
    self.typeLabel = [[UILabel alloc] init];
    [self addSubview:_typeLabel];
    self.typeLabel.textColor = [UIColor whiteColor];
    self.typeLabel.backgroundColor = [UIColor magentaColor];
    self.typeLabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    
    self.lineView = [UIView new];
    self.lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_lineView];
    
    self.workCountLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeRedCyan];
    [self addSubview:_workCountLabel];
    self.workCountLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    
    self.reViewLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeRedCyan];
    [self addSubview:_reViewLabel];
    self.reViewLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
//    [self.collectionButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.collectionButton.backgroundColor = [UIColor orangeColor];
    self.collectionButton.layer.cornerRadius = 5;
    self.collectionButton.layer.masksToBounds = YES;
    self.collectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.collectionButton.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];

    [self addSubview:_collectionButton];
    
    [self createFoot];
    
    [self p_contrains];
}
- (void)createFoot{
    self.footView = [[Team9BaseView alloc] init];
    [self addSubview:_footView];
//    self.footView.backgroundColor = [UIColor whiteColor];
    
    self.loctionLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_loctionLabel];
    
    self.costLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_costLabel];
    
    self.rangelabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_rangelabel];
    
    self.detialButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.detialButton setTitle:@"查看全部信息" forState:UIControlStateNormal];
    [self addSubview:_detialButton];
    self.detialButton.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.detialButton.layer.borderWidth = 1.0f;
    self.detialButton.layer.cornerRadius = 5.0f;
    [self.detialButton addTarget:self action:@selector(detail:) forControlEvents:UIControlEventTouchUpInside];
    
    _loctionLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    _costLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    _rangelabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
}
- (void)detail:(id)sender{
    if (self.block) {
        self.block();
    }
}
- (void)p_contrains{
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self);
        make.bottom.equalTo(self.collectionButton).offset(10);
    }];
    
    [self.faceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.height.and.width.equalTo(@(100 * WIDTH / 375));
        make.top.equalTo(self.headView).offset(10);
    }];
    self.faceImageView.layer.cornerRadius = 50 * WIDTH / 375;
    self.faceImageView.layer.masksToBounds = YES;
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.faceImageView.mas_bottom).offset(10);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@1);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(20);
    }];
    
    Team9BaseLabel *conutLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:conutLabel];
    conutLabel.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    conutLabel.text = @"套作品";
    
    Team9BaseLabel *reViewLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:reViewLabel];
    reViewLabel.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    reViewLabel.text = @"人预约过";
    
    [self.workCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView);
    }];
    [self.reViewLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView);
    }];
    [conutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.workCountLabel.mas_bottom).offset(1);
        make.centerX.equalTo(self.workCountLabel);
        make.right.equalTo(self.lineView).offset(-10);
        make.bottom.equalTo(self.lineView);
    }];
    [reViewLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lineView);
        make.centerX.equalTo(self.reViewLabel);
        make.left.equalTo(self.lineView).offset(10);
    }];
    
    [self.collectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.equalTo(self.collectionButton.mas_height).multipliedBy(2);
    }];
    [self p_contraintsFoot];
    
}
- (void)p_contraintsFoot{
    [self.footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom).offset(10);
        make.bottom.right.left.equalTo(self);
    }];
    Team9BaseLabel *loctionNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:loctionNameLabel];
    
    Team9BaseLabel *costNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:costNameLabel];
    
    Team9BaseLabel *rangeNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:rangeNameLabel];
    
    loctionNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    costNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    rangeNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    
    loctionNameLabel.text = @"服务地区";
    costNameLabel.text = @"设计收费";
    rangeNameLabel.text = @"服务范围";
    
    [loctionNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.footView).offset(10);
    }];
    
    [costNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loctionNameLabel.mas_bottom).offset(10);
        make.right.equalTo(loctionNameLabel);
    }];
    
    [rangeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(costNameLabel.mas_bottom).offset(10);
        make.right.equalTo(costNameLabel);
    }];
    
    [self.loctionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loctionNameLabel);
        make.left.equalTo(loctionNameLabel.mas_right).offset(10);
    }];
    [self.costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(costNameLabel);
        make.left.equalTo(costNameLabel.mas_right).offset(10);
    }];
    [self.rangelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rangeNameLabel);
        make.left.equalTo(rangeNameLabel.mas_right).offset(10);
    }];
    [self.detialButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rangeNameLabel.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(self.detialButton.mas_height).multipliedBy(3.5);
    }];
}
- (void)reload{
    
    self.loctionLabel.text = _serviceModel.location;
    NSString *string = [NSString stringWithFormat:@"%@~%@", _serviceModel.price_min, _serviceModel.price_max];
    self.costLabel.text = [NSString stringWithFormat:@"%@元/平米", string];
    NSMutableAttributedString *attributeString =
    [[NSMutableAttributedString alloc] initWithString:self.costLabel.text];
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor cyanColor];
    }else color = [UIColor redColor];
#pragma mark button颜色
    
    [attributeString setAttributes:@{
                                     NSForegroundColorAttributeName : color
                                     } range:[self.costLabel.text rangeOfString:string]];
    self.costLabel.attributedText = attributeString;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor grayColor];
    } else color = [UIColor orangeColor];
    [self.detialButton setTitleColor:color forState:UIControlStateNormal];
    self.collectionButton.backgroundColor = color;
    self.detialButton.layer.borderColor = color.CGColor;
    NSString *str = [NSString string];
//    NSLog(@"%@", self.serviceModel.services[0]);
    for (NSString *strPart in self.serviceModel.services) {
        str = [str stringByAppendingString:[NSString stringWithFormat:@"%@ ", strPart]];
    }
    self.rangelabel.text = str;
    
    
    [self.faceImageView sd_setImageWithURL:[NSURL URLWithString:self.userModel.face] placeholderImage:[UIImage imageNamed:@"333"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [UIView animateWithDuration:0.3 animations:^{
            CATransition *transition = [CATransition animation];
            transition.type = @"fade";
            [transition setDuration:0.5];
            [transition setRepeatCount:1];
            // 向imageView上添加动画效果
            [self.faceImageView.layer addAnimation:transition forKey:@"transition"];
        }];
    }];
    
    self.nameLabel.text = self.userModel.nick;
    switch (self.userModel.user_type.intValue) {
        case 1:
            self.typeLabel.text = @"设计师";
            break;
            
        default:
            break;
    }
    self.reViewLabel.text = self.userModel.verified_work_count;
    self.workCountLabel.text = self.userModel.contacted_count;
    self.saver = [Team9SaveModel new];
    self.saver.author_id = self.userId;
    self.saver.author_nick = self.userModel.nick;
    self.saver.author_type = @"1";
    self.saver.author_face = self.userModel.face;
    DataBaseTool *dataTool = [DataBaseTool shareDataBaseTool];
    if ([dataTool isHave:self.saver] == InTable) {
        [self.collectionButton setTitle:@"已关注" forState:UIControlStateNormal];
        [self.collectionButton addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    } else if ([dataTool isHave:self.saver] == NotInTable){
        [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
        [self.collectionButton addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    } else [self.collectionButton setTitle:@"错误" forState:UIControlStateNormal];
    
}
- (void)deleteData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] deleteDesigner:self.saver];
    [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
    [self.collectionButton addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] insertDesigner:self.saver];
    [self.collectionButton setTitle:@"已关注" forState:UIControlStateNormal];
       [self.collectionButton addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)test{
    self.faceImageView.backgroundColor = [UIColor redColor];
    self.nameLabel.text = @"我是谁";
    self.workCountLabel.text = @"36";
    self.reViewLabel.text = @"24";
    self.typeLabel.text = @"设计师";
    self.loctionLabel.text = @"china";
    self.costLabel.text = @"¥123";
    self.rangelabel.text = @"打脸";
}
@end
